package org.example.group.controller;

import jakarta.servlet.http.HttpServletRequest;
import org.example.entity.Base.Result;
import org.example.group.entity.Group;
import org.example.group.entity.GroupUser;
import org.example.group.service.GroupService;
import org.example.message.entity.Message;
import org.example.util.FileSetUtil;
import org.example.util.JWTUtil;
import org.example.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.sql.Timestamp;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/group")
@Validated
public class GroupController {
    private final GroupService groupService;

    public GroupController(GroupService groupService) {
        this.groupService = groupService;
    }

    @GetMapping("/getGroupList")
    public Result getGroupList(HttpServletRequest request) {
        String token=request.getHeader("Authorization");
        Map<String, Object> c= JWTUtil.parseToken(token);
        String UserEmail=(String)(c.get("Email"));
        List<Group> grouplist= groupService.getgrouplist(UserEmail);
        return Result.success(grouplist);
    }
    @GetMapping("/getMessageList")
    public Result getMessageList() {
        List<Message> list= groupService.messagelist();
        return Result.success(list);
    }

    @GetMapping("/getUserByGroup")
    public Result getUserByGroup(@RequestParam(name="groupId")String groupId) {
        List<GroupUser> list= groupService.getUserByGroup(groupId);
        return Result.success(list);
    }
    @GetMapping("/selectGroup")
    public Result selectGroup(@RequestParam(name="groupId")String groupId) {
        Group group= groupService.selectGroup(groupId);
        return Result.success(group);
    }

    @PostMapping("/addGroupMessage")
    public Result addGroupMessage(HttpServletRequest request, @RequestBody Map<String, String> requestBody) {
        String content = requestBody.get("content");
        String token = request.getHeader("Authorization");
        Map<String, Object> c = JWTUtil.parseToken(token);
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
// 设置纳秒部分，保留最多6位微秒
        timestamp.setNanos(timestamp.getNanos() / 1000 * 1000);
        Message message = new Message(null,(String)(c.get("Email")), null, 2, content, timestamp.toString());
        groupService.addGroupMessage(message);
        return Result.success(message);
    }

    @PostMapping ("/addGroup")
    public Result addGroup(HttpServletRequest request,
    @RequestParam("groupName") String GroupName,
    @RequestParam("picture") MultipartFile Picture ) {
        File file =  FileSetUtil.saveFile(Picture,"./external-resources/", LocalTime.now().toString());
        String imageurl= OssUtil.uploadImage(file.getName());
        String token = request.getHeader("Authorization");
        Map<String, Object> c = JWTUtil.parseToken(token);
       String GroupBullder= (String) c.get("Email");
        if (groupService.addGroup(GroupName,GroupBullder, imageurl)==1)
        return Result.success();
     else
        return Result.error("添加失败");
    }
    @PostMapping("/joinGroup")
    public Result joinGroup(HttpServletRequest request, @RequestBody Map<String, String> body) {
        Integer GroupId= Integer.parseInt(body.get("GroupId"));
        String token = request.getHeader("Authorization");
        Map<String, Object> c = JWTUtil.parseToken(token);
        String UserEmail= (String) c.get("Email");
        if (groupService.joinGroup(GroupId,UserEmail)==1)
            return Result.success();
        else
            return Result.error("加入失败");
    }
    @PostMapping("/outGroup")
    public Result outGroup(HttpServletRequest request, @RequestBody Map<String, String> body) {
        Integer GroupId= Integer.parseInt(body.get("GroupId"));
        String token = request.getHeader("Authorization");
        Map<String, Object> c = JWTUtil.parseToken(token);
        String UserEmail= (String) c.get("Email");
        if (groupService.outGroup(GroupId,UserEmail)==1)
            return Result.success();
        else
            return Result.error("加入失败");
    }
}
